package com.lefu.es.db;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.lefu.es.util.LogUtils;

public class DBOpenHelper extends SQLiteOpenHelper {
    private static final String TAG="DBOpenHelper";
    private static final String name = "lefu.db";
    private static final int version = 1;

    public DBOpenHelper(Context context) {
        super(context, name, null, version);
    }

    /**
     * 创建数据库
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        LogUtils.d(TAG,"第一次安装，新建数据库");
        /*用户表*/
        db.execSQL("DROP TABLE IF EXISTS user");
        //*用户测量记录表*//*
        db.execSQL("DROP TABLE IF EXISTS userrecord");

        db.execSQL("CREATE TABLE IF NOT EXISTS user (id integer primary key autoincrement, username varchar(30),ugroup varchar(10),"
                + "sex varchar(2),level varchar(10),bheigth real, ageyear INTEGER,agemonth INTEGER,number INTEGER,scaletype varchar(10),uniqueid varchar(10),birth varchar(20),per_photo varchar(200),targweight real ,danwei varchar(10))");

        db.execSQL("CREATE TABLE IF NOT EXISTS userrecord (id integer primary key autoincrement, useid INTEGER,scaleType varchar(10) " +
                ",ugroup varchar(10),recordtime DATETIME DEFAULT CURRENT_TIMESTAMP,comparerecord varchar(50)," +
                "combodewater,comfat,combone,combmi,comviseralfat,combmr,commuscle,comage," +
                "rweight real,rbmi real,rbone real"
                + ",rbodyfat real,rmuscle real,rbodywater real,rvisceralfat real,rbmr real,photo varchar(200),bodyage real,sync_fitbit varchar(2),fitbit_id varchar(20))");

    }

    /**
     * 更新
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        LogUtils.d("DBOpenHelper", "==oldVersion:" + oldVersion);
        LogUtils.d("DBOpenHelper", "==newVersion:" + newVersion);
        if (oldVersion < 7) {
            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "combodewater" + " real;");
            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "comfat" + " real;");
            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "combone" + " real;");
            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "combmi" + " real;");
            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "comviseralfat" + " real;");

            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "combmr" + " real;");

            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "commuscle" + " real;");

            db.execSQL(
                    "ALTER TABLE " + "userrecord" +
                            " ADD " + "comage" + " real;");

        }else if(oldVersion<8) {
            db.execSQL("ALTER TABLE userrecord ADD sync_fitbit varchar(2);");
            db.execSQL("ALTER TABLE userrecord ADD fitbit_id varchar(20)");
        }
    }
}
